﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class GameController : MonoBehaviour {

	public GameObject player;
	public GameObject[] spawnPoints;
	public GameObject alienPrefab;

	public int maxAliensOnScreen;
	public int totalAliens;
	public float minSpawnTime;
	public float maxSpawnTime;
	public int aliensPerSpawn;

	private int aliensOnScreen = 0;
	private float generatedSpawnTime = 0;
	private float currentSpawnTime = 0;


	//power up spawn

	public GameObject upgradePrefab;
	public Gun gun;
	public float upgradeMaxTimeSpawn = 7.5f;
	private bool spawnedUpgrade = false;
	private float actualUpgradeTime = 0;
	private float currentUpgradeTime = 0;

	//
	public GameObject deathFloor;

	//
	public Animator arenaAnimator;

	// Use this for initialization
	void Start () {
		actualUpgradeTime = Random.Range(upgradeMaxTimeSpawn - 3.0f, upgradeMaxTimeSpawn);
		actualUpgradeTime = Mathf.Abs(actualUpgradeTime);
	}
	
	// Update is called once per frame
	void Update () {
		if (player) {
			SpawnAlien ();
			SpawnPowerUp ();
		}
	}

	private void SpawnAlien() {
		currentSpawnTime += Time.deltaTime;
		if (currentSpawnTime > generatedSpawnTime) {
			currentSpawnTime = 0;
			generatedSpawnTime = Random.Range (minSpawnTime, maxSpawnTime);

			if (aliensOnScreen < totalAliens && aliensPerSpawn > 0) {
				List<int> previousSpawnLocations = new List<int> ();

				if (aliensPerSpawn > spawnPoints.Length) {
					aliensPerSpawn = spawnPoints.Length - 1;
				}
				aliensPerSpawn = aliensPerSpawn > totalAliens ? aliensPerSpawn - totalAliens : aliensPerSpawn;

				for (int i=0; i < aliensPerSpawn; i++) {
					if (aliensOnScreen < maxAliensOnScreen) {
						aliensOnScreen += 1;

						int spawnPoint = -1;
						while (spawnPoint == -1) {
							int randomIndex = Random.Range (0, spawnPoints.Length - 1);
							if (!previousSpawnLocations.Contains(randomIndex)) {
								previousSpawnLocations.Add (randomIndex);
								spawnPoint = randomIndex;
							}
						}

						GameObject spawnLocation = spawnPoints [spawnPoint];
						GameObject newAlent = Instantiate (alienPrefab, spawnLocation.transform.position, Quaternion.identity) as GameObject;
						Alien alienScript = newAlent.GetComponent<Alien> ();
						alienScript.target = player.transform;

						Vector3 targetRotation = new Vector3 (player.transform.position.x, newAlent.transform.position.y, player.transform.position.z);
						newAlent.transform.LookAt (targetRotation);

						alienScript.OnDestroy.AddListener (AlienDestroyed);
						alienScript.GetDeathParticle ().SetDeathFloor (deathFloor);
					}
				}
			}
		}
	}

	public void AlienDestroyed() {
		aliensOnScreen -= 1;
		totalAliens -= 1;

		if (totalAliens == 0) {
			EndGame ();
		}
	}

	private void SpawnPowerUp() {
		currentUpgradeTime += Time.deltaTime;
		if (currentUpgradeTime > actualUpgradeTime) {
			if (!spawnedUpgrade) {;
				int randomPoint = Random.Range (0, spawnPoints.Length - 1);
				GameObject spawnLocation = spawnPoints [randomPoint];

				GameObject upgrade = Instantiate (upgradePrefab) as GameObject;
				Upgrade upgradeScript = upgrade.GetComponent<Upgrade> ();
				upgradeScript.gun = gun;
				upgrade.transform.position = spawnLocation.transform.position;
				spawnedUpgrade = true;

				SoundManager.Instance.PlayOneShot (SoundManager.Instance.powerUpAppear);
			}
		}
	}

	private void EndGame() {
		arenaAnimator.SetTrigger ("PlayerWon");
		SoundManager.Instance.PlayOneShot (SoundManager.Instance.elevatorArrived);
	}
}
